<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of userAction
 * 用户登录
 * @author xlp
 */
class loginAction extends commonAction {

    function __construct() {
        parent::__construct();
    }
    /*
     * 登录
     */
    function ajaxLogin() {
        //避免重复登录
        if(parent::_checkLogin(true)) {
            $this->JsonReturn('您已登录');
        }
        $phone    = $this->_post('phone');
        $password = $this->_post('password');
        $refer    = $this->_post('refer' , '');
        if(!isHave($phone)) {
            $this->JsonReturn('请输入您的手机号');
        } 
        if(!isHave($password)) {
            $this->JsonReturn('请输入您的密码');
        }
        $where['phone']  = $phone;
        $where['status'] = 1;
        $user = M('user')->where($where)->find();
        if(!$user) {
            $this->JsonReturn('帐号不存在或已被冻结');
        }
        if(md5($password) != $user['password']) {
            $this->JsonReturn('密码错误');
        }
        D('member')->setUserLogin($user, 3600 * 24 * 30);
        $this->JsonReturn('登录成功' , urldecode($refer) , 1);
    }



    /*
     * 注册
     */
    function register() {
        if(isAjax()) {
            load('string.fun');
            $phone    = $this->_post("phone");
            $captcha  = $this->_post("captcha");
            $password_one = $this->_post("password_one");
            $password_two = $this->_post("password_two");    
            if(!isHave($phone)) {
                $this->JsonReturn('请填写您的手机号');
            }
            if(!isHave($captcha)) {
                $this->JsonReturn('请填写验证码');
            }
            if(!isHave($password_one)) {
                $this->JsonReturn('请填写密码');
            }
            if(!isHave($password_two)) {
                $this->JsonReturn('请填写确认密码');
            }
            if(!checkPhone($phone)) {
                $this->JsonReturn('请输入正确的手机号码');
            }
            //检查是否被注册
            $isReg = M('user')->field('uid')->where(array('phone'=>$phone))->find();
            if($isReg) {
                $this->JsonReturn('该手机号已注册');
            }
            //校对验证码
            if(!S("captcha_" . $phone)) {
                $this->JsonReturn('验证码已失效，请重新发送');
            }
            if($captcha != S("captcha_" . $phone)) {
                $this->JsonReturn('您输入的验证码有误');
            }
            //校验密码
            if($password_one != $password_two) {
                $this->JsonReturn('两次密码输入不一致');
            }

            if(!checkPassword($password_one)) {
                $this->JsonReturn('请输入正确长度的密码');
            }

            $regData['phone'] = $phone;
            $regData['password'] = md5($password_one);
            $regData['user_level'] = 1; //默认vip1
            $regData['reg_time'] = time();
            $regData['status'] = 1;
            $uid = D('member')->addUser($regData);
            //默认注册后直接登录
            if($uid > 0) {
                D('member')->setUserLogin(array('uid' => $uid), 3600 * 24 * 30);
                $this->JsonReturn('注册成功' , null , 1);
            }else{
                $this->JsonReturn('服务忙，请稍后再试~!');
            }
        }
    }



    //重发验证码
    function sendCaptchaRepeat() {
        $regInfo = mySession('regInfo');
        if(!isHave($regInfo)) {
            $this->JsonReturn('参数丢失');
        }
        //发送手机短信
        if($this->sendMessage($regInfo['phone'])) {
            $this->JsonReturn('短信发送成功', null , 1);
        }else{
            $this->JsonReturn('短信发送失败');
        }
    }






    /*
     * 退出登录
     */
    function loginout() {
        user::setUserLoginOut();
        jumpTo(U('index'));
    }


    /*
     * 发送手机短信
     */
    function sendMessage() {
        $phone = $this->_post('phone');
        if(!isHave($phone)) {
            $this->JsonReturn("参数丢失");
        }
        //检测短信发送数量
        // $stime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        // $etime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        // $map['uid'] = user::$userInfo['uid'];
        // $map['createtime[>=]'] = $stime;
        // $map['createtime[<=]'] = $etime;
        // $data = M('sms_log')->where($map)->count();
        // if($data >= 10) {
        //     $this->JsonReturn('您今日短信已超上限');
        // }
        //生成验证码
        $captcha = getRandInt(4);
        S("captcha_" . $phone, $captcha, 120); //2分钟内有效

        //加载阿里大鱼短信接口
        V('Sms/sms' , false);
        $data['phone']   = $phone;
        $data['captcha'] = $captcha;
        $result = sms::sendSms($data , 'SMS_80215353');
        if ($result) {
            $save['phone']      = $phone;
            $save['createtime'] = TIME;
            M('sms_log')->insert($save);
            $this->JsonReturn('ok', null, 1);
        } else {
            $error = sms::gerError();
            saveLog('login/error', array('error' => $error));
            $this->JsonReturn('服务忙，请稍后再试~!');
        }

    }


    //检测手机是否被注册
    function checkPhoneRegister() {
        $map['phone'] = $this->_post('phone');
        $data = M('user')->field('uid')->where($map)->find();
        if(!$data){
            $this->JsonReturn('ok' , null , 1);
        }else{
            $this->JsonReturn('该手机号已注册');
        }
    }


}

